package com.chezai.client.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.chezai.client.R;
import com.chezai.client.bean.TerminalBaseInfo;
import com.chezai.client.bean.TerminalParameter;
import com.chezai.client.dao.DaoManager;
import com.chezai.client.dao.manager.ClassRecordDaoManager;
import com.chezai.client.dao.manager.PeriodRecordDaoManager;
import com.chezai.client.dao.manager.TerminalBaseInfoDaoManager;
import com.chezai.client.dao.manager.TerminalParameterDaoManager;
import com.chezai.client.global.Const;
import com.chezai.client.global.DeviceFactory;
import com.chezai.client.global.NetConst;
import com.chezai.client.global.TerminalApplication;
import com.chezai.client.server.TerminalSocketClientManager;
import com.chezai.client.utils.BuildDataUtil;
import com.chezai.client.utils.DeviceUtils;
import com.chezai.client.utils.FileUtil;
import com.chezai.client.utils.MD5Util;
import com.chezai.client.utils.PreferenceManager;
import com.chezai.client.utils.ToastUtil;
import com.chezai.client.widget.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 配置页
 * Created by likunyang on 2017/10/18.
 */

public class DebugActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.et_adress)
    ClearEditText etAdress;
    @BindView(R.id.et_port)
    ClearEditText etPort;
    @BindView(R.id.et_update_url)
    ClearEditText etUpdateUrl;
    @BindView(R.id.etPhone)
    ClearEditText etPhone;
    @BindView(R.id.tv_password)
    TextView tvPassword;
    @BindView(R.id.tb_open)
    ToggleButton tbButton;
    @BindView(R.id.tb_open_verity)
    ToggleButton tbVerity;//人脸认证
    @BindView(R.id.tb_open_loclog)
    ToggleButton tbLocLog;
    @BindView(R.id.btn_open_sys_camera)
    Button btnOpenSysCamera;
    @BindView(R.id.ll_debug)
    LinearLayout llDebug;//开发时猜测功能布局
    private TerminalParameter mServiceAddress;
    private TerminalParameter mServicePort;
    private TerminalBaseInfo mTerminalBaseInfo;

    public static void openDebug(Activity from) {
        Intent intent = new Intent(from, DebugActivity.class);
        from.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_debug;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        llDebug.setVisibility(NetConst.RELEASE_SERVER ? View.GONE : View.VISIBLE);
        tbButton.setChecked(PreferenceManager.getBoolean(Const.SP_IS_OPEN_LOG_SERVICE, false));
        tbButton.setOnCheckedChangeListener(this);
        tbVerity.setChecked(PreferenceManager.getBoolean(Const.SP_IS_OPEN_VERITY, false));
        tbVerity.setOnCheckedChangeListener(this);
        tbLocLog.setChecked(PreferenceManager.getBoolean(Const.SP_IS_OPEN_LOC_LOG, false));
        tbLocLog.setOnCheckedChangeListener(this);
        if (!Const.QHYC_C103.equals(TerminalApplication.channelModel)) {
            btnOpenSysCamera.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {
        mServiceAddress = TerminalParameterDaoManager.getInstance().findTerminalParameterByKeyId(19);
        mServicePort = TerminalParameterDaoManager.getInstance().findTerminalParameterByKeyId(24);
        etAdress.setText(mServiceAddress.getValue());
        etPort.setText(mServicePort.getValue());
        mTerminalBaseInfo = TerminalBaseInfoDaoManager.getInstance().getTerminalBaseInfo();
        if (mTerminalBaseInfo != null) {
            etPhone.setText(Long.valueOf(mTerminalBaseInfo.getSim()) + "");
        }
        tvPassword.setText(MD5Util.getSystemPass());
    }

    @OnClick({R.id.btn_save_address, R.id.btn_down_load,
            R.id.btn_save_phone, R.id.btn_setting, R.id.btn_clear_cache,
            R.id.btn_clear_all, R.id.btn_open_sys_camera, R.id.btn_test_camera, R.id.btn_power,
            R.id.btn_odb, R.id.btn_speech_test,
            R.id.btn_copy, R.id.btn_insert, R.id.btn_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save_address://保存地址
                String address = etAdress.getText().toString().trim();
                String port = etPort.getText().toString().trim();
                if (TextUtils.isEmpty(address) || TextUtils.isEmpty(port)) {
                    showToast("地址或端口号 不能为空");
                } else {
                    mServiceAddress.setValue(address);
                    mServicePort.setValue(port);
                    TerminalParameterDaoManager.getInstance().getTerminalParameterDao().update(mServiceAddress);
                    TerminalParameterDaoManager.getInstance().getTerminalParameterDao().update(mServicePort);
                    TerminalSocketClientManager.getInstance().changeServerAddress(address, Integer.parseInt(port), 0, Const.ConnectionOrder.CHANGE);
                }
                break;
            case R.id.btn_save_phone:
                String phone = etPhone.getText().toString().trim();
                if (phone.length() > 10) {
                    mTerminalBaseInfo.setPhone(phone);
                    TerminalBaseInfoDaoManager.getInstance().update(mTerminalBaseInfo);
                } else {
                    showToast("号码不能为空");
                }
                break;
            case R.id.btn_down_load:
                String updateUrl = etUpdateUrl.getText().toString().trim();
                if (updateUrl.endsWith(".apk")) {
                    UpdateAppActivity.openUpdateApp(this, updateUrl);
                }
                break;
            case R.id.btn_setting:
                DeviceFactory.hardware.openSettings(this);
                break;
            case R.id.btn_clear_cache://清除使用数据
                DaoManager.getInstance().deleUseData();
                BuildDataUtil.clearCoachClassId();
                BuildDataUtil.clearStudentClassId();
                break;
            case R.id.btn_clear_all://清除全部数据
                DaoManager.getInstance().deleAllData();
                PreferenceManager.clear();
                showToast("重启应用");
                TerminalSocketClientManager.getInstance().stopSendHead();
                DeviceUtils.restartAPP(this);
                break;
            case R.id.btn_test_camera:
                CameraActivity.startCameraActivity(this, Const.TakePhotoMode.TEST_CAMERA, 0, 0, 0, "", "", Const.CameraChannel.FONT, null,null,false);
                break;
            case R.id.btn_power:
                Intent power = new Intent(this, TestPowerActivity.class);
                startActivity(power);
                break;
            case R.id.btn_odb:
                Intent blue = new Intent(this, BluetoothListActivity.class);
                startActivity(blue);
                break;
            case R.id.btn_speech_test:
                Intent speech = new Intent(this, SpeechTestActivity.class);
                startActivity(speech);
                break;
            case R.id.btn_open_sys_camera:
                Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(takeIntent);
                break;
            case R.id.btn_insert:
                ClassRecordDaoManager.getInstance().testInsert();
                PeriodRecordDaoManager.getInstance().testInsert();
                ToastUtil.showToast(this, "插入完毕");
                break;
            case R.id.btn_copy:
                FileUtil.copyDatabase(this);
                ToastUtil.showToast(this, "Copy over");
                break;
            case R.id.btn_delete:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ClassRecordDaoManager.getInstance().deleteTimeOutData();
                        PeriodRecordDaoManager.getInstance().deleteTimeOutData();
                    }
                }).start();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
        if (cb.getId() == R.id.tb_open) {
            tbButton.setChecked(isChecked);
            PreferenceManager.putBoolean(Const.SP_IS_OPEN_LOG_SERVICE, isChecked);
        } else if (cb.getId() == R.id.tb_open_verity) {
            tbVerity.setChecked(isChecked);
            PreferenceManager.putBoolean(Const.SP_IS_OPEN_VERITY, isChecked);
        } else if (cb.getId() == R.id.tb_open_loclog) {
            tbLocLog.setChecked(isChecked);
            PreferenceManager.putBoolean(Const.SP_IS_OPEN_LOC_LOG, isChecked);
        }
    }
}
